import {ConfigDataEntity} from "../../generate/entitas";
import {MemberHeadData} from "../MemberHeadData";
import {CellData} from "../CellData";
import {TypeParse} from "../TypeParse";
import excelConfig from "../../ExcelConfig";
import BaseGenerator from "../valuedecorator/generator/BaseGenerator";
import GeneratorDecorator from "../valuedecorator/GeneratorDecorator";

export class ExcelService {
	public static parseHeadData(entity: ConfigDataEntity) : MemberHeadData[] {
		let ret_list = []
		let remove_list = []
		let sheetData = entity.sheetData.value
		let ncolumns = sheetData.columnCount
		let descRow = sheetData.getRow(1)
		let nameRow = sheetData.getRow(2)
		let memberTypeRow = sheetData.getRow(3)
		let checkData = sheetData.getRow(4)
		let column = 0
		for (let i = 1; i <= ncolumns; i++) {
			var cell = descRow.getCell(i)
			let desc = utils.GetCellValue(cell) as string
			if (desc.startsWith("#")) {
				remove_list.push(i)
				continue
			}
			let mem = new MemberHeadData()
			mem.headName = desc
			mem.name = utils.GetCellValue(nameRow.getCell(i))  as string
			mem.normalType = utils.GetCellValue(memberTypeRow.getCell(i)) as string
			mem.generatorString = utils.GetCellValue(checkData.getCell(i))  as string
			mem.column = column
			mem.columnIndex = i
			column++
			ret_list.push(mem)
		}
		return ret_list
	}

	public static parseSheetCellList(entity : ConfigDataEntity) {
		try {
			let headList : Array<MemberHeadData> = entity.memberHeadDataList.value
			let sheetData = entity.sheetData.value
			let maxRow = entity.sheetData.value.rowCount
			let cellListRow = []
			for (let row = excelConfig.startIndex; row <= maxRow; row++) {
				let cellList = []
				let rowData = sheetData.getRow(row)
				for (let j = 0; j < headList.length; j++) {
					let column = headList[j].column
					let cellData = new CellData()
					cellData.cellData = rowData.getCell(headList[j].columnIndex)
					cellData.fullName = entity.fullName.value
					cellData.sheetName = sheetData.name
					cellData.name = headList[column].name
					cellData.value = utils.GetCellValue(cellData.cellData)
					cellData.memberType = headList[column].normalType
					TypeParse.ParseValue(cellData)
					cellList.push(cellData)
				}
				cellListRow.push(cellList)
			}
			return cellListRow
		} catch (e) {
			console.error(`[解析失败] parseSheetCellList ${e}, ${entity.fileName.value}`)
		}
	}

	public static createColumnEntityList(entity : ConfigDataEntity){
		let headList = entity.memberHeadDataList.value
		let columnEntityList = new Array<ConfigDataEntity>()
		for (let i = 0; i < headList.length; i++) {
			let columnEntity = context.createEntity()
			columnEntityList.push(columnEntity)
			columnEntity.replaceMemberHeadData(headList[i])
			columnEntity.replaceColumnIndex(i)
			columnEntity.replaceRootEntity(entity)
			this.parseGeneratorString(columnEntity)
		}
		entity.replaceColumnEntityList(columnEntityList)
	}

	public static parseGeneratorString(columnEntity : ConfigDataEntity){
		let generatorList = []
		let headData = columnEntity.memberHeadData.value
		let parseJson : Object = JSON.parse(headData.generatorString)
		if (parseJson == null){

		}else{
			for (let parseJsonKey in parseJson) {
				generatorList.push(this.getCheckWithName(parseJsonKey, parseJson[parseJsonKey], columnEntity))
			}
		}
	}

	public static GetColumnDataList(columnEntity : ConfigDataEntity) : Array<CellData>{
		var root = columnEntity.rootEntity.value
		var ret = new Array<any>()
		var rowCellList = root.rowCellList.value
		var column = columnEntity.columnIndex.value
		for (let i = 0; i < rowCellList.length; i++) {
			var rowCell = rowCellList[i]
			ret.push(rowCell[column])
		}
		return ret;

	}

	public static getCheckWithName(key : string, value : object, columnEntity : ConfigDataEntity) : BaseGenerator{
		let generator  = GeneratorDecorator.getGeneratorWithName(key)
		// @ts-ignore
		let ret = new generator(key, value, columnEntity)
		ret.init()
		return ret
	}
}